package com.wanyang.controller;

import com.wanyang.pojo.Cloud;
import com.wanyang.pojo.User;
import com.wanyang.service.AccountService;
import com.wanyang.util.ResponseData;
import com.wanyang.util.Status;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * Created by zhai on 16/4/18.
 */
@Controller
public class AccountController {

    private AccountService accountService;

    @Autowired
    public void setAccountService(AccountService accountService) {
        this.accountService = accountService;
    }

    @RequestMapping(value = "/account", method = RequestMethod.GET)
    public String getAccountView(Model model, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if(null == user) {
            return "server/pages/admin-login";
        }
        model.addAttribute("userId", user.getUserId());
        model.addAttribute("username", user.getUsername());
        model.addAttribute("headImg", null == user.getHeadImg()
                ? "http://img.sc115.com/uploads/sc/jpgs/1504/apic10833_sc115.com.jpg"
                : user.getHeadImg());
        return "server/pages/admin-account";
    }

    @RequestMapping(value = "/account/list", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData getAccountList(HttpSession session) {
        ResponseData responseData = new ResponseData();
        User user = (User) session.getAttribute("user");
        responseData.setStatus(Status.success);
        responseData.setData(accountService.getAccountList(user.getUserId()));
        return responseData;
    }

    @RequestMapping(value = "/account/delete", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData deleteCloud(@RequestParam("cloudId") Integer cloudId) {
        ResponseData responseData = new ResponseData();
        responseData.setStatus(accountService.deleteCloud(cloudId) > 0 ? Status.success : Status.error);
        return responseData;
    }

    @RequestMapping(value = "/account/add", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData addCloud(@RequestParam("account") String account,
                                 @RequestParam("password") String password,
                                 @RequestParam("editType") Integer editType,
                                 @RequestParam("cloudType") Integer cloudType,
                                 @RequestParam(value = "cloudId", required = false) Integer cloudId,
                                 HttpSession session) {
        ResponseData responseData = new ResponseData();
        User user = (User) session.getAttribute("user");
        Cloud cloud;
        if(editType == 1) {
            cloud = accountService.getById(cloudId);
        } else {
            cloud = new Cloud();
            cloud.setCreateTime(new Date());
            cloud.setUserId(user.getUserId());
        }
        cloud.setAccount(account);
        cloud.setPassword(password);
        cloud.setCloudType(cloudType);
        responseData.setStatus(accountService.save(cloud, editType) ? Status.success : Status.error);
        return responseData;
    }
}
